﻿@{
    ViewBag.Title = "分配用户";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var CompanyId = GetQuery('CompanyId');                //公司主键
    var CompanyName = unescape(GetQuery('CompanyName'));  //公司名称
    var ObjectId = GetQuery('ObjectId');                  //对象主键
    var Category = GetQuery('Category');                  //分类
    $(function () {
        $("#layoutA").splitter({
            type: "v",
            outline: true,
            minLeft: 180, sizeLeft: 180, maxLeft: 180,
            anchorToWindow: true,
            accessKey: "L"
        });
        GetTree();
        GetList();
    })
    //加载根据公司ID查询 部门信息
    function GetTree() {
        var root = {
            "id": "",
            "text": CompanyName,
            "value": CompanyId,
            "isexpand": true,
            "hasChildren": true,
            "img": "/Content/Images/Icon16/house.png"
        };
        var treedata = [];
        AjaxJson("/CommonModule/Department/DepartmentTreeJson", { CompanyId: CompanyId }, function (data) {
            treedata = data;
        });
        var o = {
            onnodeclick: function (item) {
                if (item.id != "") {
                    $("#CenterTitle").html(item.text + " - " + $('.' + item.id).length + "人");
                    $('.sys_spec_text li').hide();
                    $('.' + item.id).show();
                    var a = $('.' + item.id).length;//个数
                    var b = $('.' + item.id).find('.a_selected').length;//选中个数
                    if (a != 0 && a == b) {
                        $("#CheckButton").attr('title', '反选');
                        $("#CheckButton").text('反选');
                        $("#CheckButton").attr('class', 'checkAllOn');
                    } else {
                        $("#CheckButton").attr('title', '全选');
                        $("#CheckButton").text('全选');
                        $("#CheckButton").attr('class', 'checkAllOff');
                    }
                } else {
                    $('.sys_spec_text li').show();
                    $("#CenterTitle").html("所有成员 - " + $('.sys_spec_text li').length + "人");
                }
            }
        };
        root["ChildNodes"] = treedata;
        o.data = [root];
        $("#ItemsTree").treeview(o);
    }
    //加载用户
    function GetList() {
        getAjax("/CommonModule/Permission/MemberList", { CompanyId: CompanyId, ObjectId: ObjectId, Category: Category }, function (data) {
            $(".sys_spec_text").html(data);
        });
        $("#CenterTitle").html("所有用户 - " + $('.sys_spec_text li').length + "人");
        $("#MemberList li").click(function () {
            if (!$(this).attr('disabled')) {
                if (!!$(this).hasClass("selected")) {
                    $(this).removeClass("selected");
                    $(this).find('a').removeClass("a_selected");
                } else {
                    $(this).addClass("selected").siblings("li");
                    $(this).find('a').addClass("a_selected");
                }
            }
        });
        //自定义复选框 全选/反选
        $("#CheckButton").click(function () {
            var DepartmentId = "";
            if (!!$('.bbit-tree-selected').attr('id')) {
                DepartmentId = $('.bbit-tree-selected').attr('id').substring(10).replace(/_/g, '-');
            }
            if (!!$(this).hasClass("checkAllOff")) {
                $(this).attr('title', '反选');
                $(this).text('反选');
                $(this).attr('class', 'checkAllOn');
                if (DepartmentId != "") {
                    $('.' + DepartmentId).addClass('selected');
                    $('.' + DepartmentId).find('a').addClass('a_selected');
                } else {
                    $('.sys_spec_text li').addClass('selected');
                    $('.sys_spec_text li').find('a').addClass('a_selected');
                }
            } else {
                $(this).attr('title', '全选');
                $(this).text('全选');
                $(this).attr('class', 'checkAllOff');
                if (DepartmentId != "") {
                    $('.' + DepartmentId).removeClass('selected');
                    $('.' + DepartmentId).find('a').removeClass('a_selected');
                } else {
                    $('.sys_spec_text li').removeClass('selected');
                    $('.sys_spec_text li').find('a').removeClass('a_selected');
                }
            }
        });
        //模糊查询用户（注：这个方法是理由jquery查询）
        $("#txt_Search").keyup(function () {
            if ($(this).val() != "") {
                var Search = $(this).val();
                window.setTimeout(function () {
                    $(".sys_spec_text li")
                     .hide()
                     .filter(":contains('" + (Search) + "')")
                     .show();
                }, 200);
            } else {
                $(".sys_spec_text li").show();
            }
        }).keyup();
    }
    //保存事件
    function AcceptClick() {
        Loading(true, "正在提交数据...");
        window.setTimeout(function () {
            var UserId = ""; $('.sys_spec_text .selected a').each(function () { UserId += $(this).attr('id') + ","; });
            var postData = { UserId: UserId, ObjectId: ObjectId, Category: Category, }
            AjaxJson("/CommonModule/Permission/AuthorizedMember", postData, function (data) {
                Loading(false);
                tipDialog(data.Message, 3, data.Code);
                top.frames[tabiframeId()].windowload();
                closeDialog();
            });
        }, 200);
    }
</script>
<div class="note-prompt" style="margin: 1px;">
    温馨提示：选中复选框即可授予相应权限，取消选中则回收相应权限。
</div>
<div id="layoutA" class="layout" onselectstart="return false;" style="-moz-user-select: none;">
    <!--左边-->
    <div class="layoutPanel layout-west">
        <div class="btnbartitle">
            <div>
                归属部门
            </div>
        </div>
        <div class="ScrollBar" id="ItemsTree"></div>
    </div>
    <!--中间-->
    <div class="layoutPanel layout-center">
        <div class="btnbartitle">
            <div style="float: left">
                归属用户 - <span id="CenterTitle"></span>；用户查询：<input id="txt_Search" type="text" class="btnbartitleinput" style="width: 120px;" />
            </div>
            <div style="float: right">
                <label id="CheckButton" class="checkAllOff" title="全选">全选</label>
            </div>
        </div>
        <div class="ScrollBar" id="MemberList">
            <ul class="sys_spec_text">
            </ul>
        </div>
    </div>
</div>
